<?php

namespace app\admin\controller;

use app\common\controller\BaseAdmin;
use app\admin\model\Withdraw as Model;


class Withdraw  extends BaseAdmin
{
    //需要获取的列表自定义字段内容，内容模型内定义
    protected $searchField =  [
        'user'=>['name'=>'username','phone','bank_number']
    ];

    /**
     * 初始化CURD控制器
     * @return void
     */
    public function initialize(){
        parent::initialize();
        $this->model = new Model();
    }


    /**
     * 列表Assign设置
     * 向index方法传模板页面参数
     */
    protected function indexAssign($data){
        if ($this->request->isAjax()){
            $total_amount = $this->model->withSearch($this->searchFields($data),$this->searchData($data))->sum('amount');
            $day_amount = $this->model->withSearch($this->searchFields($data),$this->searchData($data))
                ->whereTime('create_time', 'today')
                ->sum('amount');
            $day_pending = $this->model->withSearch($this->searchFields($data),$this->searchData($data))
                ->whereTime('create_time', 'today')
                ->where('status',0)
                ->sum('amount');
            return [
                'total_amount' =>  $total_amount,
                'day_amount' => $day_amount,
                'day_pending' => $day_pending
            ];
        }
    }

    /**
     * 添加编辑分组
     */
    public function edit(){
        if ($this->request->isAjax()){
            $id = $this->request->param('id');
            $model=$this->model::where('id', $id)->find();
            $model->state = $this->request->param('state');
            if ($model->save()){
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }

        $id = $this->request->get('id');
        if ($id){
            $list = $this->model::where('id', $id)->find();
            $this->assign('list', $list);
        }
        return $this->fetch();
    }

    public function deny(){
        $id = $this->request->param('id');
        $withdraw=$this->model::where('id', $id)->find();
        $withdraw->state = -1;
        $withdraw->completed_time = date('Y-m-d H:i:s');
        $withdraw->save();

        //更改余额
        UserService::amountAction($withdraw->user_id,$withdraw->amount,'withdraw.refuse');
        $this->success('操作成功');
    }

    /**
     * 提现通过
     */
    public function pass(){
        $id = $this->request->param('id');
        if (is_array($id)){
            $lists = $this->model->where('id', 'in', $id)->where('state',0)->select();
            try {
                foreach ($lists as $order) {
                    WithdrawService::pass($order);
                }
                $this->success('批量操作成功');
            }catch (\Exception $e) {
                $this->error('操作成功，若长时间没有结果请通知通道重新回调一下');
            }
        }else{
            $order =$this->model->where('id', $id)->where('state',0)->find();
            return json(WithdrawService::pass($order));
        }
    }

    /**
     * 提现失败
     */
    public function fail(){
        $id = $this->request->param('id');
        return json(WithdrawService::withdrawFail($id));
    }

    /**
     * 回退重新回调
     * @return void
     */
    public function back(){
        $id = $this->request->param('id');
        $order = $this->model->find($id);
        if ($order->state != 2){
            $this->error('状态错误，只能操作已通过的提现申请');
        }
        $order->state = 1;
        $order->completed_time = null;
        $order->save();
        $this->success('操作成功');
    }


    public function finish(){
        $id = $this->request->param('id');
        WithdrawService::finish($id);
        $this->success('操作成功');
    }

}